--################# Header
include("weapons/gmod_tool/stargate_base_tool.lua");
TOOL.Category="Janus's Lantis Tech";
TOOL.Name="#Zero Point Module Mk2";

TOOL.ClientConVar["autolink"] = 1;
TOOL.ClientConVar["autoweld"] = 1;
-- The default model for the GhostPreview
TOOL.ClientConVar["model"] = "models/zup/zpm/zpm.mdl";

-- Information about the SENT to spawn
TOOL.Entity.Class = "Zero_Point_Module_Mk2";
--TOOL.Entity.Limit = 2;

-- Add the topic texts, you see in the upper left corner
if(StarGate.HasWire) then
	TOOL.Topic["name"] = "ZPM Mk2 Spawner";
else
	TOOL.Topic["name"] = "Please Install Wire to use the ZPM Mk2";
end
TOOL.Topic["desc"] = "Creates a Zero Point Module Mk2";
TOOL.Topic[0] = "Left click, to spawn a ZPM Mk2";
-- Adds additional "language" - To the end of these files, the string "_*classname*" will be added, using TOOL.Entity["class"]. 
-- E.g. TOOL.Language["Undone"] will add the language "Undone_prop_physics" when TOOL.Entity["class"] is "prop_physics"
TOOL.Language["Undone"] = "Zero Point Module Mark II removed";
TOOL.Language["Cleanup"] = "Zero Point Module Mark II's";
TOOL.Language["Cleaned"] = "Removed all Zero Point Module Mark II's";
TOOL.Language["SBoxLimit"] = "Hit the Zero Point Module Mark II limit";
--################# Code

--################# LeftClick Toolaction @aVoN
function TOOL:LeftClick(t)
	if(not StarGate.HasResourceDistribution and not StarGate.HasWire) then return false end;
	if(t.Entity and t.Entity:IsPlayer()) then return false end;
	if(t.Entity and t.Entity:GetClass() == self.Entity.Class) then return false end;
	if(CLIENT) then return true end;
	--if(not self:CheckLimit()) then return false end;
	self.Max = MckaysPack.Ents.ZeroPointModuleMK2.Max or 2;   --Now set in the Config File for this addon, lua/autorun/MckaysPack.lua
	local p = self:GetOwner();
	--######## Spawn SENT
	if (MckaysPack.Ents.ZeroPointModuleMK2.Count < self.Max) then
		local e = self:SpawnSENT(p,t);
		MckaysPack.Ents.ZeroPointModuleMK2.Count = MckaysPack.Ents.ZeroPointModuleMK2.Count+1;
		if(util.tobool(self:GetClientNumber("autolink"))) then
			self:AutoLink(e,t.Entity); -- Link to that energy system, if valid
		end
		--######## Weld things?
		local c = self:Weld(e,t.Entity,util.tobool(self:GetClientNumber("autoweld")));
		--######## Cleanup and undo register
		self:AddUndo(p,e,c);
		self:AddCleanup(p,c,e);
		return true;
	else
		return false;
	end
end

--################# Controlpanel @aVoN
function TOOL:ControlsPanel(Panel)
	if(StarGate.HasResourceDistribution and StarGate.HasWire) then
		Panel:CheckBox("Autolink","zpm2_autolink"):SetToolTip("Autolink this to resouce using Entities?");
		Panel:CheckBox("Autoweld","zpm2_autoweld");
	else
		Panel:Help("The Zero Point Module Mk2 perfectly fits into Resource Distribution, LifeSupport and Wire.\nYou seem not to have all these installed.\nSo the ZPM Mk2 is 'quite useless for you' :)");
		--Dont you just love aVoN's england!
	end
end

--################# Register Stargate hooks. Needs to be called after all functions are loaded!
	TOOL:Register();
